//
//  GoogleMaps.m
//  rrqs
//
//  Created by Apple on 2017/12/19.
//

#import "GoogleMaps.h"

#define GoogleMapsAPPURLScheme @"comgooglemaps://" // 谷歌地图标识
#define GoogleMapsAPPCallbackURLScheme @"com.xyrr.rrqs.googlemaps" // 与Info.plist中设置的一致

@implementation GoogleMaps
    
/**
 检测谷歌地图是否已安装
 
 @return YES - 已安装，NO - 未安装
 */
+ (BOOL)isGoogleMapsInstalled
{
    NSURL *GoogleMapsURL = [NSURL URLWithString:GoogleMapsAPPURLScheme];
    return [[UIApplication sharedApplication] canOpenURL:GoogleMapsURL];
}
    
    /**
     跳转谷歌地图，开启导航，默认从当前位置导航到目标位置
     
     @param coordinate 目的地坐标
     */
+ (void)openGoogleMapsToCoordinate:(CLLocationCoordinate2D)coordinate
{
    NSString *da = [NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude];
    [self openGoogleMapsWithSa:@"" da:da];
}
    
/**
 跳转谷歌地图，开启导航，从起始位置导航到目标位置
 
 @param coordinate1 起始地坐标
 @param coordinate2 目的地坐标
 */
+ (void)openGoogleMapsFrom:(CLLocationCoordinate2D)coordinate1 to:(CLLocationCoordinate2D)coordinate2
{
    NSString *sa = [NSString stringWithFormat:@"%f,%f", coordinate1.latitude, coordinate1.longitude];
    NSString *da = [NSString stringWithFormat:@"%f,%f", coordinate2.latitude, coordinate2.longitude];
    [self openGoogleMapsWithSa:sa da:da];
}
    
/**
 跳转谷歌地图，开启导航

 @param sa 起始地坐标字符串
 @param da 目的地坐标字符串
 */
+ (void)openGoogleMapsWithSa:(NSString *)sa da:(NSString *)da
{
    // 必须的参数 APP的名称和Info.plist中设置的URL Scheme
    NSString *source = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    NSString *success = [GoogleMapsAPPCallbackURLScheme copy];
    
    NSString *URLString = [NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=%@&daddr=%@&directionsmode=driving", source, success, sa, da];
    URLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *targetURL = [NSURL URLWithString:URLString];
    if ([[UIApplication sharedApplication] canOpenURL:targetURL]) {
        [[UIApplication sharedApplication] openURL:targetURL];
    }
}

@end
